/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import tools.dbconnector;

/**
 *
 * @author Muhammad Anis
 */
public class user {
    //data-data yang dimiliki oleh seorang user

    //data teman yang dimiliki oleh seorang user
    List<String> friends = new ArrayList<String>();
    //data room yang dimoederasi pleh seorang user
    List<String> moderated = new ArrayList<String>();
    //data user yang sedang chatting dengan user ini
    public List<String> ChatWithFriend = new ArrayList<String>();
    //data room yang sedang dimasuki saat ini
    List<String> room = new ArrayList<String>();
    //info diri
    public String nama;
    public String username;
    public String email;
    public int maxroom;

    //konstruktor
    public user(String username) {
        //ambil nama
        dbconnector db = new dbconnector();
        String[] hasil = db.getData(username);
        if (hasil != null) {
            this.nama = hasil[0];
            this.username = username;
            this.email = hasil[1];
            this.maxroom = Integer.parseInt(hasil[2]);
        }
        //dapatkan friends yang ada
        this.friends = db.getFriends(username);
        //data yang lain masih kosong
    }

    public void processCommand(String command) {
    }

    public void printFriends(JspWriter out) {
        updateFriends();
        dbconnector db = new dbconnector();
        String isOnline;
        String kelas = "genap";
        try {
            if (friends.isEmpty()) {
                out.print("You Have No Friends Currently");
            } else {
                for (int i = 0; i < friends.size(); i++) {
                    if (i % 2 != 0) {
                        kelas = "ganjil";
                    } else {
                        kelas = "genap";
                    }
                    if (db.isOnline(friends.get(i))) {
                        isOnline = "online";
                    } else {
                        isOnline = "offline";
                    }
                    out.println("<div onclick=\"addChatTab('" + friends.get(i) + "','friend');\" class=\"" + kelas + " " + isOnline + "\">"
                            + friends.get(i) + "</div>");
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(user.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateFriends() {
        dbconnector db = new dbconnector();
        this.friends.clear();
        this.friends = db.getFriends(this.username);
    }

    public void addFriends(String username) {
        dbconnector db = new dbconnector();
        db.addFriend(this.username, username);
    }

    public void addChatFriend(String username) {
        if (!ChatWithFriend.contains(username)) 
                ChatWithFriend.add(username);
    }

    public List<String> isUpdateFriend(List<String> newList) {
        //cari semua friend di dalam list ini yang tidak ada dalam list dia
        List<String> ret = new ArrayList<String>();
        for (String friend : newList) {
            if (!ChatWithFriend.contains(friend)) {
                ChatWithFriend.add(friend);
                ret.add(friend);
            }
        }
        return ret;
    }

    public void printAllChatTab(JspWriter out) {
        try {
            if (ChatWithFriend.isEmpty()) {
                out.print("");
            } else {
                for (String ret : ChatWithFriend) {

                    out.print(ret + " ");

                }
            }
        } catch (IOException ex) {
            Logger.getLogger(user.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteFriend(){
        ChatWithFriend.clear();
    }
}
